package com.codrim.cs.data.rpc.configuration;

import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ConsumerConfig;
import com.alibaba.dubbo.config.ReferenceConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.codrim.cs.data.rpc.AdvertiserDataService;
import com.codrim.cs.data.rpc.AffiliateDataService;
import com.codrim.cs.data.rpc.ConversionsService;
import com.codrim.cs.data.rpc.CooperatorsStatService;
import com.codrim.cs.data.rpc.OfferDataService;
import com.codrim.cs.data.rpc.OfferReportService;
import com.codrim.cs.data.rpc.ReportRecordService;
import com.codrim.cs.data.rpc.ReportService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;

import javax.annotation.Resource;

/**
 * Created by Liang.Zhuge on 24/08/2017.
 */
public class CsDataDubboConfiguration {
    private static final Logger logger = LoggerFactory.getLogger(CsDataDubboConfiguration.class);

    @Resource ApplicationContext ac;

    @Bean
    public AdvertiserDataService advertiserDataService(ApplicationConfig application, RegistryConfig registry) {
        return createRpcService(application, registry, AdvertiserDataService.RPC_VERSION, AdvertiserDataService.class);
    }

    @Bean
    public AffiliateDataService affiliateDataService(ApplicationConfig application, RegistryConfig registry) {
        return createRpcService(application, registry, AffiliateDataService.RPC_VERSION, AffiliateDataService.class);
    }

    @Bean
    public CooperatorsStatService cooperatorsStatService(ApplicationConfig application, RegistryConfig registry) {
        return createRpcService(application, registry, CooperatorsStatService.RPC_VERSION, CooperatorsStatService.class);
    }

    @Bean
    public OfferDataService offerDataService(ApplicationConfig application, RegistryConfig registry) {
        return createRpcService(application, registry, OfferDataService.RPC_VERSION, OfferDataService.class);
    }

    @Bean
    public OfferReportService offerReportService(ApplicationConfig application, RegistryConfig registry) {
        return createRpcService(application, registry, OfferReportService.RPC_VERSION, OfferReportService.class);
    }

    @Bean
    public ReportRecordService reportRecordService(ApplicationConfig application, RegistryConfig registry) {
        return createRpcService(application, registry, ReportRecordService.RPC_VERSION, ReportRecordService.class);
    }

    @Bean
    public ReportService reportService(ApplicationConfig application, RegistryConfig registry) {
        return createRpcService(application, registry, ReportService.RPC_VERSION, ReportService.class);
    }

    @Bean
    public ConversionsService conversionsService(ApplicationConfig applicationConfig, RegistryConfig registryConfig) {
        return createRpcService(applicationConfig, registryConfig,
                ConversionsService.RPC_VERSION, ConversionsService.class);
    }

    private <T> T createRpcService(ApplicationConfig application, RegistryConfig registry, String versions, Class<T> type) {
        ConsumerConfig consumerConfig = null;
        try {
            consumerConfig = ac.getBean(ConsumerConfig.class);
        } catch (BeansException e) {
            logger.warn(e.getMessage());
        }
        ReferenceConfig<T> reference = new ReferenceConfig<>();
        reference.setVersion(versions);
        reference.setApplication(application);
        reference.setRegistry(registry);
        reference.setInterface(type);
        if (consumerConfig != null) {
            reference.setCheck(consumerConfig.isCheck());
        }
        return reference.get();
    }

}
